#!/usr/bin/tclsh

set str "The value of Pi is : "
set Pi 3.1415

puts $str

# puts "the value of Pi is : $Pi"
puts "$str $Pi"

puts "This String comes out\
on a single line"

puts {$str $pi};				# not substitute

set y [set x "def"];			# [] get the results of a command, like ` in shell
puts "Remember that set returns the new value of the variable: X: $x Y: $y\n"


puts "==================== math command ===================="
set x 100
set y 256
set z [expr {$y + $x}];			# the expression is "expr {$y + $x}"
puts z
puts "The square root of $y is [expr {sqrt($y)}]"

set a(1) 10
set a(2) 7
set a(3) 17
set b    2
puts "Sum: [expr {$a(1)+$a($b)}]"

puts "==================== branch and loop ===================="
set x 1
if {$x == 2} {
	puts "$x is 2"
} else {
	puts "$x is not 2"
}

if "$x < 5" {
	puts "$x < 5"
}


set x 0
while "$x < 5" {
	set x [expr {$x+1}]
	if "$x > 7" break
	if "$x > 3" continue
	puts "x is $x"
 }
puts "exited loop with X equal to $x"

for {set i 0} {$i < 4} {incr i} {
	puts "for loop : $i"
}

puts "==================== procedure ===================="
proc sum {arg1 arg2} {
	set x [expr {$arg1+$arg2}]
	return $x
}

puts "The sum of 2 + 3 is : [sum 2 3]"

puts "==================== list ===================="
set list1 "a b c"
set list2 [split "1987,7,22" ","]
puts "item at index 2 of list1 is : [lindex $list1 2]"
puts $list2
set i 0
foreach j $list2 {
	puts "list2\[$i\] : $j"
	incr i
}
set list1 "a b c"
#concatenate
set r [concat a b {c d e} {f}]
puts "After Concated: $r"
set r [concat a b {c d e} {f {g h}}]
puts "After Concated: $r"
# append
lappend list1 d e
puts "After lappending: $list1"
lappend list1  {f g}
puts "After lappending: $list1"
# insert
set b [linsert $list1 2 "1 2"]               ;# "1 2 3" is a single element
puts "After linsert at position 2: $b"
# replace
# set b [lreplace $b 3 5 "AA" "BB" "CC"]
set b [lreplace $b 3 5 "AA" "BB"]
puts "After lreplacing 3 positions with 2 values at position 3: $b"
#sort
set r [lsort $b];				# set b [lsort $b]		## sort b in place 
puts "After sorted : $r"
# search & range ??

# length
puts "the length of $r : [llength $r]"


puts "==================== string ===================="
set str "    this is my test string    "
puts "There are [string length $str] chars in '$str'"
puts "TO UPPER : [string toupper $str]"
puts "after trim : [string trim $str]"

set str [format "%-20s %+s" "hello" "world"]
puts $str

puts "==================== Associative Arrays ===================="
set name(first) "Zhang"
set name(last) "Kangkang"

puts "Full name: $name(first) $name(last)"
puts "Array size : [array size name]"

puts "==================== File Access ===================="
set infile [open "exp1.tcl" r]
set contents [read -nonewline $infile]
# set contents [read $infile]
close $infile

# puts "the contents: \n $contents"
set clist [split $contents "\n"]
puts "total lines : [llength $clist]"

## Running other programs from Tcl - exec, open ??

puts "==================== Others ===================="
# cd ..
puts [pwd]
set curtime [clock format [clock seconds] -format %H:%M:%S]
set curdate [clock format [clock seconds] -format "%Y/%m/%d %H:%M:%S"]
puts "current time : $curtime"
puts "current date : $curdate"

set datestr "Jun 9, 2013"
set testday [clock scan $datestr]
puts "$datestr -> $testday -> [clock format $testday -format %D]"

